package data;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import models.OwnerDomainModel;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

public class OwnerDomainData {
    private Objectify ofy;
    
    private int limit = 20;
    
    private Cache cache = null;
    
    private String prefix = "owner_";
    
    private int page = 1;
    
    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public OwnerDomainData() {
        try {
            ObjectifyService.register(OwnerDomainModel.class);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        
        try {
            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
        } catch (CacheException e) {
           //e.printStackTrace();
        }
        
        ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
        ofy = ObjectifyService.begin(opts);
    }
    
    public ArrayList<OwnerDomainModel> getVerifyDomainChange() {
        ArrayList<OwnerDomainModel> domains = new ArrayList<OwnerDomainModel>();
        
        Calendar cal=Calendar.getInstance();
		Date now = new Date();
		cal.setTime(now);
		cal.add(Calendar.DATE, -3);
        
        int start = (page - 1) * limit;
        Query<OwnerDomainModel> q = ofy.query(OwnerDomainModel.class).filter("isUpdated =", 0).filter("lastUpdate <=", cal.getTime()).limit(limit).offset(start);
        for (OwnerDomainModel domain: q) {
            domains.add(domain);
        }
        
        return domains;
    }
    
    public ArrayList<OwnerDomainModel> getVertifyOwnerDomain(String username) {
        ArrayList<OwnerDomainModel> domains = new ArrayList<OwnerDomainModel>();
        
        Query<OwnerDomainModel> q = ofy.query(OwnerDomainModel.class).filter("username", username).filter("status", 1);
        for (OwnerDomainModel domain: q) {
            domains.add(domain);
        }
        
        return domains;
    }
    
    public ArrayList<OwnerDomainModel> getUnvertifyOwnerDomain(String username) {
        ArrayList<OwnerDomainModel> domains = new ArrayList<OwnerDomainModel>();
        
        Query<OwnerDomainModel> q = ofy.query(OwnerDomainModel.class).filter("username", username).filter("status", 0);
        for (OwnerDomainModel domain: q) {
            domains.add(domain);
        }
        
        return domains;
    }
    
    public OwnerDomainModel getOwnerDomain(String id) {
        OwnerDomainModel ownerDomain = new OwnerDomainModel();
        
        if (cache != null && cache.containsKey(prefix + id)) {
            ownerDomain = (OwnerDomainModel) cache.get(prefix + id);
        } else {    
            try {
                ownerDomain = ofy.get(new Key<OwnerDomainModel>(OwnerDomainModel.class, id));
                cache.put(prefix + id, ownerDomain);
            } catch (Exception e) {
                ownerDomain = null;
            }
        }
        return ownerDomain;
    }
    
    public OwnerDomainModel getByDomain(String domainName) {
        OwnerDomainModel ownerDomain = new OwnerDomainModel();
        if (cache != null && cache.containsKey(prefix + domainName)) {
            ownerDomain = (OwnerDomainModel) cache.get(prefix + domainName);
        } else {
            try {
                ownerDomain = ofy.query(OwnerDomainModel.class).filter("domain", domainName).get();
                cache.put(prefix + domainName, ownerDomain);
            } catch (Exception e) {
                ownerDomain = null;
            }
        }
        return ownerDomain;
    }
    
    public boolean checkExists(String id) {
        boolean exists = false;
        OwnerDomainModel ownerModel = getOwnerDomain(id);
        if (ownerModel != null) {
            exists = true;
        }
        
        return exists;
    }
    
    public void insert(OwnerDomainModel ownerDomain) {
        if (cache != null) {
            cache.put(prefix + ownerDomain.getId(), ownerDomain);
            cache.put(prefix + ownerDomain.getDomain(), ownerDomain);
        }
        
        ofy.put(ownerDomain);
    }
    
    public void delete(OwnerDomainModel ownerDomain) {
        if (cache != null && cache.containsKey(prefix + ownerDomain.getId())) {
            cache.remove(prefix + ownerDomain.getId());
        }
        
        if (cache != null && cache.containsKey(prefix + ownerDomain.getDomain())) {
            cache.remove(prefix + ownerDomain.getDomain());
        }
        ofy.delete(ownerDomain);
    }
}
